package com.htu.tax.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htu.tax.entity.Mailaddress;
import com.htu.tax.mapper.TAddressMapper;
import com.htu.tax.payload.request.Address.AddAddressRequest;
import com.htu.tax.payload.request.Address.GetAddressRequest;
import com.htu.tax.payload.request.Address.UpdateAddressRequest;
import com.htu.tax.payload.base.PageRequest;
import com.htu.tax.service.ITAddressService;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
@CrossOrigin
public class ITAddressServiceImpl extends ServiceImpl<TAddressMapper, Mailaddress> implements ITAddressService {
    @Resource
    private TAddressMapper addressMapper;

    @CrossOrigin
    @Override
    public void addAddress(AddAddressRequest address) {
        Snowflake snowflake = cn.hutool.core.util.IdUtil.createSnowflake(1, 1);
        Mailaddress maddress = new Mailaddress();
        maddress.setId(snowflake.nextId())
                .setName(address.getName())
                .setPhone(address.getPhone())
                .setProvince(address.getProvince())
                .setCity(address.getCity())
                .setCounty(address.getCounty())
                .setMaddress(address.getMaddress())
                .setCreatetime(LocalDateTime.now());
        addressMapper.add(maddress);
    }

    @CrossOrigin
    @Override
    public IPage<Mailaddress> getAddress(GetAddressRequest addressRequest) {
        return addressMapper.get(PageRequest.getPage(addressRequest));
    }

    @Override
    public void updateAddress(@RequestBody UpdateAddressRequest address) {
        Mailaddress m1 = this.getById(Long.valueOf(address.getId()));
        m1.setUpdatetime(LocalDateTime.now());
        addressMapper.updateA(m1.getId(),
                address.getName(),
                address.getPhone(),
                address.getNewprovince(),
                address.getNewcity(),
                address.getNewcounty(),
                address.getNewaddress(),
                m1.getUpdatetime());

    }

}
